package com.iyao_app.mvp.search;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.amap.api.maps.model.LatLng;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.iyao_app.R;
import com.iyao_app.adapter.SearchAdapter_;
import com.iyao_app.base.BaseFragment;
import com.iyao_app.entry.HomeData;
import com.iyao_app.entry.Manor;
import com.iyao_app.entry.PageInfo;
import com.iyao_app.entry.Pois;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.mvp.dialog.DialogSceneActivity;
import com.iyao_app.util.Contants;
import com.iyaowoqu.base_library.util.LogUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/30.
 */

public class SearchResultFragment extends BaseFragment implements OnRefreshLoadmoreListener {
    @BindView(R.id.rc_nearly_nearly_hot)
    RecyclerView mRcNearlyNearlyHot;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    Unbinder unbinder;
    private final int pageSize = 20;
    private boolean isRefresh = true;
    private int page = 1;

    private String city;
    private int type;
    private Manor manor;

    private SearchAdapter_ hotAdapter;

    private LinearLayoutManager hotManager;

    String keyWord;
    private PageInfo pageInfo;
    private final int REQUEST_CODE = 0;

    @Override
    public View initRootView(LayoutInflater inflater) {
        rootView = mInflater.inflate(R.layout.fragment_search_result, null);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isRefresh = true;
    }

    @Override
    public void setViews() {
        city = getArguments().getString(Contants.OTHER_FIELD);
        LogUtil.e("=city===city=="+city);
        manor = (Manor) getArguments().getSerializable(Contants.THIRD_FIELD);
        type = getArguments().getInt(Contants.CLASS_TYPE, 0);
        LatLng latLng = new LatLng(manor.latitude, manor.longitude);
        hotAdapter = new SearchAdapter_(null, latLng);
        hotAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        hotAdapter.isFirstOnly(true);
        mRcNearlyNearlyHot.setAdapter(hotAdapter);
        hotManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRcNearlyNearlyHot.setLayoutManager(hotManager);
    }

    @Override
    public void initData() {
        mRefreshLayout.setOnRefreshLoadmoreListener(this);
        hotAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Pois item = (Pois) adapter.getItem(position);
                if (item != null) {
                    String location = item.getLocation();
                    if (TextUtils.isEmpty(location)) {
                        return;
                    }
                    String[] split = location.split(",");

                    manor.latitude = Double.parseDouble(split[1]);
                    manor.longitude = Double.parseDouble(split[0]);
                    manor.name = item.getName();
                    manor.sceneId = item.getSceneId();
                    manor.typecode = item.getTypecode();
                    Intent intent = null;
                    if (type == 0) {
                        intent = new Intent(getActivity(), DialogSceneActivity.class);
                        intent.putExtra(Contants.THIRD_FIELD, manor);
                        startActivityForResult(intent, REQUEST_CODE);
                        getActivity().overridePendingTransition(R.anim.dialog_activity_enter, 0);
                    } else {
                        intent = new Intent();
                        intent.putExtra(Contants.TYPE, manor);
                        ((SearchActivity) getActivity()).setResult(SearchActivity.RESULT_CODE, intent);
                        finish();
                    }

                }
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void search(String keyWord,String city) {
        this.keyWord = keyWord;
        if (!isRefresh) {
            if (pageInfo != null) {
                if (pageInfo.pageTotal > page) {
                    page++;
                } else {
                    page = 1;
                }
            }
        } else {
            page = 1;
        }
        NetUtils.getInstance().keyword(page, pageSize, city, keyWord, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        HomeData homeData = model.getModel();
                        pageInfo = homeData.getPageInfo();
                        List<Pois> pois = homeData.getPois();
                        if (pageInfo.pageTotal <= page) {
                            mRefreshLayout.finishLoadmoreWithNoMoreData();
                        } else {
                            mRefreshLayout.finishLoadmore();
                        }
                        if (isRefresh) {
                            mRefreshLayout.finishRefresh();
                        } else {
                            mRefreshLayout.finishLoadmore();
                        }
                        if (pois == null || pois.size() == 0) {
                            mRefreshLayout.finishLoadmoreWithNoMoreData();
                            LogUtil.e("=====list.size() == 0====");
                        } else {
                            if (isRefresh) {
                                hotAdapter.replaceData(pois);
                                mRefreshLayout.finishRefresh();
                            } else {
                                hotAdapter.addData(pois);
                                mRefreshLayout.finishLoadmore();
                            }
                        }
                    }

                }
            }

            @Override
            public void onFail(String message, int code) {
                mRefreshLayout.finishRefresh();
                mRefreshLayout.finishLoadmore();
            }
        }, HomeData.class);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        isRefresh = false;
        search(keyWord,city);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        isRefresh = true;
        search(keyWord,city);
    }
}
